package com.redpack.redpost.struts.action.admin;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.redpack.redpost.struts.form.admin.multimediaForm;
import com.redpack.redpost.util.dao.adminDAO;
import com.redpack.redpost.util.to.generalUtil;

public class agregamultimediaval extends Action {
	
	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form, 
			HttpServletRequest request, 
			HttpServletResponse response) throws Exception {
		
		boolean logginVal = generalUtil.isLoggedInAdmin(request);
		if(logginVal!=true){
			return mapping.findForward("noLogin") ;
		}
		
		multimediaForm f = (multimediaForm)form;
		String archivo = "";
		String nombreArchivo = "",
				extension = "",
				tipoMultimedia = "Tienda", IdFile = "";
		boolean val = false;
		if(f != null){
			
			nombreArchivo = f.getImagen( ).toString( ) ;
			if( f.getImagen() != null && !nombreArchivo.equals("") ){
				
				int i = nombreArchivo.lastIndexOf(".") ;
				int cveTeserSlide = 0 ;
				if( f.getTipo( ) == 1 ){
					
					if( f.getSlide( ) > 0 ){
						cveTeserSlide = f.getSlide( ) ;
						tipoMultimedia = "Slide" ;
					}else
						if( f.getTeaser( ) > 0 ){
							cveTeserSlide = f.getTeaser( ) ;
							tipoMultimedia = "Teaser" ;
						}
					
				}
	    		if( i > 0 )
	    			extension = nombreArchivo.substring(i+1) ;
				
					IdFile = generalUtil.nombreArchivoMultimedia(tipoMultimedia, cveTeserSlide, extension) ;
		    		
		    		FormFile firstLocalFile = f.getImagen( ) ;
		    		
		    		String root = getServlet().getServletContext().getRealPath("") ;
		    		root = root.replaceFirst(".\\\\tmp\\\\", "") ;
		    		int indice = root.indexOf("tmp") ;
		    		String nr = root.substring(0, indice) ;
		    		nr = nr + "Multimedia.war" ;
		    		
		    		System.out.println("Ruta== " + nr ) ;
		    		
		            File path = new File(nr) ;
		            if(!path.exists()){
		            	path.mkdir();
		    	    }
		            
		            File uploadedFile = new File(path + "/" + IdFile) ;
		            uploadedFile.createNewFile( ) ;
		            
		            FileOutputStream fos = new FileOutputStream(uploadedFile);
		            
		            byte[] contentInBytes = firstLocalFile.getFileData() ; // content.getBytes();
		            
			        fos.write(contentInBytes);
			        fos.flush();
			        fos.close();
			        archivo = IdFile;
			}
			
			if( f.getCve_multimedia() > 0 ){
				val = adminDAO.updMultimedia( f.getCve_multimedia(), f.getTeaser() , f.getSlide(), f.getNombre(), archivo, f.getTipo(), f.getLink(), f.getText(), f.getOrden(), f.getCategoria(), f.getQuitar_imagen() );
				if( f.getTipo() == 2 ){
					return mapping.findForward("successEditTienda");
				}else{
					return mapping.findForward("successEdit") ;
				}
			}else{
				val = adminDAO.saveMultimedia( f.getTeaser() , f.getSlide(), f.getNombre(), archivo, f.getTipo(), f.getLink(), f.getText(), f.getOrden(), f.getCategoria() );
			}
		}
		
		List slides = new ArrayList();
		List teasers = new ArrayList();
		try {
			slides = adminDAO.getSlides();
			teasers = adminDAO.getTeaser();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		request.setAttribute("slides", slides);
		request.setAttribute("teasers", teasers);
		
		if( val == true ){
			request.setAttribute("correcto", ""+val);
		}else{
			request.setAttribute("correcto", "");
		}
		
		if( f.getTipo( ) == 2 ){
			return mapping.findForward("success2") ;
		}else{
			return mapping.findForward("success") ;
		}
		
	}

}